<script lang="tsx">
/**
 * 客户管理
 */
export default {
  name: "customer"
}
</script>

<script lang="tsx" setup>
import {ref} from "vue"
import {definePageModel, PageModel} from "@/frame/view/layout/page-model/constructor.ts"
import CustomerModel from "./CustomerModel.ts"
import APIS from "@/apis"
import {getCode} from "@/utils/get-code.ts"
import {CodeRuleTypeEnum} from "@/utils/enum.ts"

/**
 * 页面模型
 */
const pageModelRef = ref()
const config = definePageModel<CustomerModel>({
  url: APIS.CUSTOMER_BASE,
  searchForm: {
    els: [
      {
        eType: "el-input",
        prop: "code",
        props: {
          placeholder: "客户编码",
          clearable: true
        }
      },
      {
        eType: "el-input",
        prop: "name",
        props: {
          placeholder: "客户名称",
          clearable: true
        }
      },
      {
        eType: "el-input",
        prop: "phone",
        props: {
          placeholder: "联系电话",
          clearable: true
        }
      }
    ]
  },
  programForm: {
    els: [
      {
        label: "新增",
        eType: "el-button",
        props: {
          type: "primary"
        },
        event: "add"
      }
    ]
  },
  table: {
    page: {
      enable: true,
      pageSize: 20
    },
    props: {
      stripe: true,
      border: true
    },
    operation: {
      width: 100,
      els: [
        {
          eType: "el-button",
          label: "编辑",
          event: "edit",
          props: {
            type: "primary",
            link: true
          },
          hide: false
        },
        {
          eType: "el-button",
          label: "删除",
          event: "delete",
          props: {
            type: "primary",
            link: true
          },
          hide: false
        }
      ]
    },
    els: [
      {
        label: "编码",
        prop: "code",
        minWidth: 120
      },
      {
        label: "名称",
        prop: "name",
        minWidth: 120
      },
      {
        label: "性别",
        prop: "sex",
        minWidth: 120
      },
      {
        label: "年龄",
        prop: "age",
        minWidth: 120
      },
      {
        label: "联系方式",
        prop: "phone",
        minWidth: 120
      },
      {
        label: "创建时间",
        prop: "createTime",
        minWidth: 160
      }
    ]
  },
  form: {
    title: "新增/编辑",
    width: 500,
    sheetForm: {
      async bindData(data: any) {
        if (!data?.id) {
          data.code = await getCode(CodeRuleTypeEnum.客户管理表)
        }
        return data
      },
      labelWidth: "100px",
      els: [
        {
          label: "编码",
          eType: "el-input",
          prop: "code",
          renderFn(data) {
            return <span>{data.code}</span>
          }
        },
        {
          label: "名称",
          eType: "el-input",
          prop: "name",
          props: {
            placeholder: "客户名称",
            clearable: true
          }
        },
        {
          label: "性别",
          eType: "el-input",
          prop: "sex",
          props: {
            placeholder: "客户性别",
            clearable: true
          }
        },
        {
          label: "年龄",
          eType: "el-input",
          prop: "age",
          props: {
            placeholder: "客户年龄",
            clearable: true
          }
        },
        {
          label: "联系方式",
          eType: "el-input",
          prop: "phone",
          props: {
            placeholder: "客户联系方式",
            clearable: true
          }
        }
      ]
    }
  }
})

/**
 * 新增
 */
const handleAdd = () => {
  pageModelRef.value.handleAdd()
}

/**
 * 编辑
 */
const handleEdit = (row: any) => {
  pageModelRef.value.handleEdit(row)
}

/**
 * 删除
 */
const handleDelete = (row: any) => {
  pageModelRef.value.handleDelete(row)
}
</script>

<template>
  <PageModel ref="pageModelRef" :PageModel="config" @add="handleAdd" @delete="handleDelete" @edit="handleEdit"/>
</template>

<style lang="scss" scoped>
</style>